package com.omt.tarjimdinek.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.inject.Named;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePartDataSource;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;


/**
 * Sending an email
 * @author chabane
 *
 */
public class MailHelper{

    private static final Logger log = Logger.getLogger(MailHelper.class);
    
	private static ResourceBundle msg = ResourceBundle.getBundle("localization.messages");
    
    private static final String SMTP_HOST = "smtp.gmail.com";
    private static final String LOGIN_GMAIL = "chabane.refes@gmail.com";
    private static final String PASSWORD_GMAIL = "counterakarfs";
	/**
	 * Send an email
	 * @param lUtilisateur
	 */
	public static void envoyerEmail(String aTo, String aCC, String aFrom, String aSubject, String aText, String aAttachment,
			Class aClass) {
		
		Properties lProperties = new Properties();
		lProperties.put("mail.smtp.host", SMTP_HOST);
		lProperties.put("mail.smtp.socketFactory.port", "465");
		lProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		lProperties.put("mail.smtp.auth", "true");
		lProperties.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(lProperties,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(LOGIN_GMAIL, PASSWORD_GMAIL);
				}
			});
 
		try {
 
			Message lMessage = new MimeMessage(session);
			lMessage.setFrom(new InternetAddress(aFrom));
			lMessage.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(aTo));
			lMessage.setSubject(StringHelper.encodeToUTF8(aSubject));
			
			// Build message
			String lText = aText;
			lText = MessageFormat.format(msg.getString(Constant.MAIL_MODEL), lText);	
			
			MimeMultipart lMultipart = new MimeMultipart("related");
			
			// Add content 
			BodyPart lMessageBodyPart = new MimeBodyPart();
		    lMessageBodyPart.setContent(lText, "text/html");
		    lMultipart.addBodyPart(lMessageBodyPart);
	        
		    // Add banner image
		    URL lBannerURL = aClass.getClassLoader().getResource("img/banner.png");

		    lMessageBodyPart = new MimeBodyPart();
		    DataSource lBanner = new FileDataSource(lBannerURL.getFile());
		    lMessageBodyPart.setDataHandler(new DataHandler(lBanner));
		    lMessageBodyPart.addHeader("Content-ID","<banner>");
		    lMultipart.addBodyPart(lMessageBodyPart);

		    // Add footer image
		    URL lFooterURL = aClass.getClassLoader().getResource("img/footer.png");
		    
		    lMessageBodyPart = new MimeBodyPart();
		    DataSource lFooter = new FileDataSource(lFooterURL.getFile());
		    lMessageBodyPart.setDataHandler(new DataHandler(lFooter));
		    lMessageBodyPart.addHeader("Content-ID","<footer>");
		    lMultipart.addBodyPart(lMessageBodyPart);
		    
		    // Send message
		    lMessage.setContent(lMultipart);
			Transport.send(lMessage);
 
			log.info("Sent message successfully, to "+aTo);
 
		} catch (MessagingException e) {
	    	log.error(e.getMessage());
		}
	}
}
